package com.psjj.privilege.web.servlet;

import com.google.gson.Gson;
import com.psjj.privilege.po.Privilege;
import com.psjj.privilege.po.Role;
import com.psjj.privilege.service.PrivilegeService;
import com.psjj.privilege.service.RoleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/role.do")
public class RoleController extends BasicServlet{
    private RoleService  roleService = new RoleService();
    private PrivilegeService privilegeService = new PrivilegeService();
    private static final String BASIC_URL = "/WEB-INF/page/role/";
    //查询页面
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询所有角色数据
        List<Role> list = roleService.queryRoles();
        //将数据存储到req域中
        req.setAttribute("list",list);
        //转发到角色列表页面
        req.getRequestDispatcher(BASIC_URL+"table.jsp").forward(req,resp);
    }
    //配置角色添加页面
    private void addUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //转发到添加页面
        req.getRequestDispatcher(BASIC_URL+"add.jsp").forward(req,resp);
    }
    //保存添加数据
    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得提交数据
        String rname = req.getParameter("rname");
        String description = req.getParameter("description");
        //封装到po
        Role role = new Role();
        role.setDescription(description);
        role.setRname(rname);
        //保存到数据库
        roleService.add(role);
        //重定向到查询页面
        resp.sendRedirect(req.getContextPath()+"/role.do?method=list");
    }
    //单条查询
    private void editUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得rid值
        int rid = Integer.parseInt(req.getParameter("rid"));
        //根据id查询角色数据
        Role role = roleService.queryRoleById(rid);
        //将数据保存req域对象中
        req.setAttribute("role",role);
        //转发到修改页面
        req.getRequestDispatcher(BASIC_URL+"update.jsp").forward(req,resp);
    }
    //修改数据
    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得提交的数据
        int rid = Integer.parseInt(req.getParameter("rid"));
        String rname = req.getParameter("rname");
        String description = req.getParameter("description").trim();
        //封装到bean
        Role role = new Role();
        role.setRid(rid);
        role.setRname(rname);
        role.setDescription(description);
        //修改到数据库
        roleService.editRole(role);
        //重定向到查询页面
        resp.sendRedirect(req.getContextPath()+"/role.do?method=list");
    }
    //删除数据
    private void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得提交的数据
        int rid = Integer.parseInt(req.getParameter("rid"));
        //根据id删除数据
        roleService.del(rid);
        //重定向到查询页面
        resp.sendRedirect(req.getContextPath()+"/role.do?method=list");
    }
    //跳转至权限页面
    private void privilegeUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得rid数据
        int rid = Integer.parseInt(req.getParameter("rid"));
        //查询角色数据
        Role role = roleService.queryRoleById(rid);
        //将role信息保存req域
        req.setAttribute("role",role);
        req.getRequestDispatcher(BASIC_URL+"privilege.jsp").forward(req,resp);
    }
    //获取角色的权限数据
    private void privilegeData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置响应格式
        resp.setContentType("application/json;charset=utf-8");
        //获得rid数据
        int rid = Integer.parseInt(req.getParameter("rid"));
        //获取权限数据并关联角色所拥有的权限数据
        List<Privilege> list = privilegeService.queryPrivilegesJoinRoleHasPrivilege(rid);
        //将数据转为json格式
        Gson gson = new Gson();
        String result = gson.toJson(list);
        //响应数据
        resp.getWriter().println(result);
    }
    //保存菜单数据
    private void privilegeSave(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //获得rid数据
            int rid = Integer.parseInt(req.getParameter("rid"));
            //获取选中权限id
            String midsStr = req.getParameter("mids");
            //保存数据库数据
            privilegeService.saveRoleIdAndMids(rid,midsStr);
            //响应数据
            resp.getWriter().println(1);
        } catch (NumberFormatException e) {
            resp.getWriter().println(0);
            e.printStackTrace();
        }
    }
}
